package XZK010205;

import XZK010205.bean.Express;
import XZK010205.service.ExpressService;
import XZK010205.view.Views;

/**
 * 快递E站-入口类
 * 面向对象+异常处理
 *
 * @author bing.z 2021/5/30 16:54
 **/
public class ExpressDemo {
    // 快递E站-操作类
    private static ExpressService expressService = new ExpressService();
    // 操作面板
    private static Views views = new Views();

    public static void main(String[] args) {
        views.welcome();
        m:
        while (true) {
            int no = views.mainMenu();
            switch (no) {
                case 0:
                    break m;
                case 1: {
                    adminClient();
                }
                break;
                case 2: {
                    userClient();
                }
                break;
                default:
                    break;
            }
        }
        views.bye();
    }

    /**
     * 用户客户端
     */
    private static void userClient() {
        m:
        while (true) {
            int no = views.userMenu();
            switch (no) {
                case 0:
                    break m;
                case 1: {
                    int code = views.takeExpress();
                    Express express = expressService.takeByCode(code);
                    if (express != null) {
                        // 取出快递,这里简单打印
                        System.out.println(express);
                    } else {
                        System.out.println("取件码有误!");
                    }
                }
                break;
                default:
                    break;
            }
        }
    }

    /**
     * 快递员客户端
     */
    private static void adminClient() {
        m:
        while (true) {

            int no = views.adminMenu();
            switch (no) {
                case 0:
                    break m;
                case 1: {
                    Express express = views.insert();
                    Express insert = expressService.insert(express);
                    if (insert != null) {
                        views.success();
                    } else {
                        views.fail();
                    }
                }
                break;
                case 2: {
                    String number = views.delete();
                    boolean delete = expressService.delete(number);
                    if (delete) {
                        views.success();
                    } else {
                        views.fail();
                    }
                }
                break;
                case 3: {
                    String number = views.getNumberOrCompany("请输入需要更新快递的单号:");
                    Express oldExpress = expressService.getByNumber(number);
                    Express updateExpress = views.update(oldExpress);
                    boolean update = expressService.update(updateExpress);
                    if (update) {
                        views.success();
                    } else {
                        views.fail();
                    }
                }
                break;
                case 4: {
                    Express[][] expresses = expressService.getAll();
                    views.showAll(expresses);
                }
                break;
                default:
                    break;
            }
        }
    }
}
